using MediatR;
using Microsoft.AspNetCore.Mvc;
using WMS.ErrorCode;
using WMS.WebApiWrite.Application.Command;
using WMS.WebApiWrite.DTO;

namespace WMS.WebApiWrite.Controllers
{
    /// <summary>
    /// 仓库信息写操作控制器（新增、修改、删除）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<WarehouseController> logger;

        /// <summary>
        /// 构造函数，注入MediatR和日志服务
        /// </summary>
        public WarehouseController(IMediator mediator, ILogger<WarehouseController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 新增仓库信息
        /// </summary>
        /// <param name="warehouseDTO">仓库信息DTO</param>
        /// <returns>返回操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> AddWarehouse([FromBody] WarehouseDTO warehouseDTO)
        {
            logger.LogInformation($"开始新增仓库信息，仓库名称：{warehouseDTO.WarehouseName}，数据状态：{warehouseDTO.DataStatus}");

            return await mediator.Send(new AddWarehouseCommand
            {
                WarehouseDTO = warehouseDTO
            });
        }

        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="warehouseDTO">仓库信息DTO</param>
        /// <returns>返回操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<bool>> UpdateWarehouse([FromBody] WarehouseDTO warehouseDTO)
        {
            logger.LogInformation($"开始修改仓库信息，仓库ID：{warehouseDTO.WarehouseID}，数据状态：{warehouseDTO.DataStatus}");

            return await mediator.Send(new UpdateWarehouseCommand
            {
                WarehouseDTO = warehouseDTO
            });
        }

        /// <summary>
        /// 删除仓库信息（逻辑删除）
        /// </summary>
        /// <param name="warehouseID">仓库ID</param>
        /// <param name="operatorName">操作人名称</param>
        /// <returns>返回操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> DeleteWarehouse([FromQuery] int warehouseID, [FromQuery] string operatorName)
        {
            logger.LogInformation($"开始删除仓库信息，仓库ID：{warehouseID}，操作人：{operatorName}");

            return await mediator.Send(new DeleteWarehouseCommand
            {
                WarehouseID = warehouseID,
                OperatorName = operatorName
            });
        }
    }
}

